<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Slide;
use OSS\OssClient;

/**
 * 幻灯管理
 */
class SlideController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'slide');
    }
    /**
     * 幻灯片列表页面
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function slide_list(Request $request)
    {
        $result = Slide::where('del', '=', '1')->orderBy('updated_at', 'DESC')->paginate(15);
        foreach ($result  as $k=>$v){
            $result[$k]['img'] = config('alioss.host').'/'.$result[$k]['img'];

        }
        return view('admin/user/slide_list', compact('result'));
    }

    /**
     * 幻灯片添加
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function add_slide_info(Request $request)
    {
        //$result = RealUser::where('del','=','1')->paginate(15);


        return view('admin/user/add_slide', compact('real'));
    }

    /**
     * 幻灯片列表页面
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function add_slide(Request $request)
    {
        $oss = new OssClient(config('alioss.accessKeyId'), config('alioss.accessKeySecret'), config('alioss.endpoint'));
        $data['title'] = $request->post('title');
        $data['order'] = $request->post('order');
        if (empty($data['title']) || empty($data['order'])) {
            return back()->with(['msg'=>'请填写完整的数据','status'=>0,'msg_url'=>'add_slide']);
        }

//        if (!empty($request->post('webpage'))){
            $data['webpage'] = $request->post('webpage')??'';
//        }
        if ($request->has('id')) {
            if ($request->has('logo')) {
                $logo = $request->file('logo');
                if ($logo) {
                    if ($logo->isValid()) {
                        $realPath = $logo->getRealPath(); //临时文件的绝对路径
                            $ext      = $logo->getClientOriginalExtension(); // 扩展名
                            $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                            $osspath  = 'block_chain/' . $filename; //oss路径
                            $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云
                            $data['img'] = $osspath; //数据库保存全路径文件名
                    }
                }
            }
            $id = $request->post('id');
            $result =  Slide::updateOrCreate(
                    [
                        'id' => $id,
                    ],
                    $data
                );
            if ($result) {
                return back()->with(['msg'=>'修改成功','status'=>1,'msg_url'=>'slide_list']);
            } else {
                return back()->with(['msg'=>'修改失败','status'=>0,'msg_url'=>'slide_list']);
            }
        } else {
            if ($request->has('logo')) {
                $logo = $request->file('logo');
                if ($logo) {
                    if ($logo->isValid()) {
                        $realPath = $logo->getRealPath(); //临时文件的绝对路径
                            $ext      = $logo->getClientOriginalExtension(); // 扩展名
                            $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                            $osspath  = 'block_chain/' . $filename; //oss路径
                            $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云
                            $data['img'] = $osspath; //数据库保存全路径文件名
                    }
                }
            } else {
                return back()->with(['msg'=>'请上传幻灯片图片','status'=>0]);
            }
            $data['del'] = 1;
            $result = Slide::create($data);
            if ($result) {
                return back()->with(['msg'=>'添加成功','status'=>1,'msg_url'=>'slide_list']);
            } else {
                return back()->with(['msg'=>'添加失败','status'=>0,'msg_url'=>'slide_list']);
            }
        }
    }

    /**
     * 幻灯片修改数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function editor_slide(Request $request)
    {
        $id = $request->get('id');
        $real = Slide::where('id', '=', $id)->first();
        $real['img'] = config('alioss.host').'/'.$real['img'];

        return view('admin/user/editor_slide', compact('real', 'id'));
    }
    /**
     * 幻灯片删除
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function del_slide(Request $request)
    {
        $id = $request->get('id');
        $real = Slide::where('id', '=', $id)->update(['del' => 2]);
        if ($real) {
            return back()->with(['msg'=>'删除成功','status'=>1,'msg_url'=>'slide_list']);
        } else {
            return back()->with(['msg'=>'删除失败','status'=>0,'msg_url'=>'slide_list']);
        }
    }
}
